/*

This file is part of Rooms3D.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
Rooms3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
Rooms3D is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Rooms3D; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/
#pragma once


#define _WIN32_DCOM

#include <comdef.h>
#include <Wbemidl.h>
#include <gdiplus.h>
#include <fstream>
#include <tchar.h>
#include <wchar.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <objbase.h>
#include <shellapi.h>
#include <thumbcache.h>
#include <GdiPlus.h>


using namespace std;
using namespace Gdiplus;


HINSTANCE GetHInstance();

class CThumbnailExtractor
{

	basic_string<WCHAR> m_wsFullPath;
	basic_string<WCHAR> m_wsFileName;
	basic_string<WCHAR> m_wsTemporaryFolderPath;

	LPITEMIDLIST pidlFileURL;
	LPITEMIDLIST pidlWorkingDirectory;

	IShellFolder* psfDesktop;
	IShellFolder* psfWorkingDirectory;
	IShellItem* psiFileItem;
	
	IExtractImage* peiIExtractImage;
	IExtractIcon* peiIExtractIcon;
	IThumbnailProvider *ptpThumbnailProvider;

	CComPtr<IMalloc> m_pMalloc;

	HWND desktopWindow;
	HDC hdc;
	HBITMAP thumbnailBitmap;
	HINSTANCE hInstance;
	BYTE *ANDmaskIcon;
	BYTE *XORmaskIcon;
	HICON thumbnailIcon;
	CComPtr<IStream> spStream;

	HRESULT hrCritical;

private:
	
	BOOL CreateURLShortcut(LPCWSTR pszURL, LPCWSTR pszShortcutFile);
	BOOL SplitAbsolutePath(LPCWSTR pwsFileAbsolutePath, basic_string<wchar_t>&  wsFileName, basic_string<wchar_t>& wsFullPath);
	int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
	
public:
	CThumbnailExtractor(void);
	~CThumbnailExtractor();
	
	/*
		Neither of these methods is supposed to be called more than once or one after another.
		This class supports only one time initialization, calling one of these methods and destruction.
	*/
	HRESULT ExtractFileThumbnail(LPCWSTR pwsFileAbsolutePath, DWORD dwWidth, DWORD dwHeight, DWORD colorDepth, HBITMAP& thumbnailImage, HICON& icon);
	HRESULT ExtractFileThumbnailIntoPNG(LPCWSTR pwsFileAbsolutePath, DWORD dwWidth, DWORD dwHeight, DWORD colorDepth, long& bufferSize, char* pngBuffer);
};